<template>
  <div class="app-container">
    <!-- <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="备件种类名称">
          <el-input
            v-model="searchParam.kindName"
            placeholder="请输入备件种类名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div> -->

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        row-key="id"
        :indent="30"
        :tree-props="{
          children: 'nodes',
          hasChildren: 'hasChildren',
        }"
        :header-cell-style="{ 'text-align': 'center' }"
      >
        <!-- <el-table-column
          label="备件种类编号"
          prop="assetTypeCode"
          min-width="150"
          show-overflow-tooltip
        /> -->
        <el-table-column
          label="备件种类名称"
          prop="kindName"
          min-width="500"
          show-overflow-tooltip
        />

        <el-table-column
          label="排序"
          prop="sort"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />

        <el-table-column
          label="启用状态"
          prop="enabled"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.enabled === 1">启用</el-tag>
            <el-tag type="info" v-else>停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="350">
          <template #default="scope">
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="添加子备件"
              placement="top"
            >
              <el-button
                type="success"
                size="small"
                link
                @click="openDialog(scope.row, 'child')"
              >
                <i-ep-plus />
              </el-button>
            </el-tooltip>
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="查看"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="openDialogdetail(scope.row)"
                ><i-ep-search />
              </el-button>
            </el-tooltip>
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="编辑"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="openDialog(scope.row)"
                ><i-ep-edit
              /></el-button>
            </el-tooltip>
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="删除"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="handleTableDelete(scope.row.id)"
                ><i-ep-delete />
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total ? pageable.total : 0"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      /> -->
    </el-card>

    <sparePartKind-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      :parent-id="addDialog.parentId"
      @reset-table="(id:string) => (id?.length ? getTableList() : getTableList())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "EAMEQUSPAREPARTKIND",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/eam/parePartsType/getTree";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {};

const deleteParam = {
  api: "/eam/parePartsType/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
  parentId: "0",
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/eam/parePartsType/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

function openDialog(roleObj?: any, type?: any) {
  if (roleObj && !type) {
    // 编辑
    addDialog.title = `编辑【${roleObj.kindName}】信息`;
    getInfo(roleObj.id);
    addDialog.parentId = "";
    addDialog.parentId = "";
  } else if (roleObj && type) {
    // 添加子机构
    addDialog.title = `【${roleObj.kindName}】添加子备件种类`;
    addDialog.parentId = roleObj.id;
    addDialog.data = {};
  } else {
    // 新增
    addDialog.data = {};
    addDialog.title = "新建备件种类";
    addDialog.parentId = "";
  }
  addDialog.visible = true;
}

/** 打开备件种类表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = `【${roleObj.kindName}】详情`;
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  addDialog.readvisible = false;
  if (!value) {
    addDialog.data = {};
  }
}

onMounted(() => {
  getTableList();
});
</script>

<style scoped lang="scss">
.search-container {
  :deep(.el-form-item__label) {
    width: 100px !important;
  }
}
</style>
